package com.ifgi.sitcom.campusmapperNDrawer.controllers;

import java.util.ArrayList;

import org.osmdroid.util.GeoPoint;

import com.gamificationlibrary.model.*;
import com.ifgi.sitcom.campusmapperNDrawer.gamedata.PlayerAdapter;
import com.ifgi.sitcom.campusmapperNDrawer.outdoordata.Building;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff.Mode;
import android.util.Log;

public class CampusMapperApplication extends Application{

	private static CampusMapperApplication instance;
	private ArrayList<Ownership<Building>> owners_Building;
	private PlayerAdapter player;
	private Area[] area=null;
	private int[] area_color;
	
    
    public static CampusMapperApplication getInstance() {
        return instance;
    }
	   
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }
  
    //begin get and set
	public ArrayList<Ownership<Building>> getOwners_Building() {
		return owners_Building;
	}

	public void setOwners_Building(ArrayList<Ownership<Building>> owners_Building) {
		this.owners_Building = owners_Building;
	}

	public int[] getArea_color() {
		return area_color;
	}

	public void setArea_color(int[] area_color) {
		this.area_color = area_color;
	}

	public PlayerAdapter getPlayer() {
		return player;
	}

	public void setPlayer(PlayerAdapter player) {
		this.player = player;
	}
	//end get and set
	
  public void setAreas()
    {
    	area=new Area[2];
    	area[0]=setArea("Muenster left area",new GeoPoint(51.9850000, 7.584827),new GeoPoint(51.9850000, 7.6090000),
    			new GeoPoint(51.940016,7.6090000),new GeoPoint(51.940016,7.584827));
    	area[1]=setArea("Muenster centre area",new GeoPoint(51.9850000, 7.6092000),new GeoPoint(51.9850000,7.634573),
    			new GeoPoint(51.940016,7.634573),new GeoPoint(51.940016,7.6091000));
    	setArea_color(new int[]{Color.parseColor("#CD80F2"),Color.parseColor("#368E22")});
    	//old areas
    	//area[0]=setArea("Orleans Ring",new GeoPoint(51.978557, 7.584827),new GeoPoint(51.978557,7.6090000),new GeoPoint(51.964809,7.6090000),new GeoPoint(51.964809,7.584827));
    	//area[1]=setArea("Rishon-Le-Zion-Ring",new GeoPoint(51.964809, 7.586879),new GeoPoint(51.964809, 7.6090000),new GeoPoint(51.956100,7.6090000),new GeoPoint(51.956100, 7.586879));
    	//area[2]=setArea("Muenster Centrum",new GeoPoint(51.9679758,  7.6090000),new GeoPoint(51.9679758, 7.634573),new GeoPoint(51.956100, 7.634573),new GeoPoint(51.956100, 7.6090000));
    	//area[3]=setArea("Aasee zone",new GeoPoint(51.956100, 7.605472),new GeoPoint(51.956100, 7.628573),new GeoPoint(51.940016, 7.628573),new GeoPoint(51.940016, 7.605472));
   	    //area[4]=setArea("Suburbs",new GeoPoint(51.9850000,  7.6090000),new GeoPoint(51.9850000, 7.628573),new GeoPoint(51.9679758, 7.628573),new GeoPoint(51.9679758, 7.6090000));
    }
  
	  public Bitmap getRoundedCornerBitmap(Bitmap bitmap,int w,int h) {
	      if(bitmap==null)
	              return null;
	      if(w>1000 || h>1000)
	              return bitmap;
	      if(w<1 || h<1)
	              return bitmap;
	     
	      /*was Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARG_8888);*/
	   Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_4444);
	   Canvas canvas = new Canvas(output);
	   
	
	
	   final Paint paint = new Paint();
	   final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
	   final RectF rectF = new RectF(0,0,bitmap.getWidth(), bitmap.getHeight());
	   final float roundPx = 7;
	
	   paint.setAntiAlias(true);
	   canvas.drawARGB(0, 255, 255,255);
	   paint.setColor(0xff424242);
	   canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
	
	   paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	   canvas.drawBitmap(bitmap, rect, rectF, paint);
	   
	   
	   paint.setStyle(Style.STROKE);
	   paint.setColor(Color.WHITE);
	  
	
	   paint.setStrokeWidth(2);
	   paint.setAntiAlias(true);
	   canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
	   
	   return output;             
	}
  
	  public String takeEmailfromSmartphone()
		{
			final AccountManager manager = AccountManager.get(this.getApplicationContext());
			final Account[] accounts = manager.getAccountsByType("com.google");	
			return accounts[0].name;
			
		}
  

  
	  public Area getArea(int number)
	  {
	  	return area[number-1];
	  }

	private Area setArea(String name, GeoPoint p1, GeoPoint p2,GeoPoint p3,GeoPoint p4) {
		// TODO Auto-generated method stub
		ArrayList<GeoPoint> geometry=new ArrayList<GeoPoint>();
		geometry.add(p1);
		geometry.add(p2);
		geometry.add(p3);
		geometry.add(p4);
		Area area =new Area(name,geometry);
	
		return area;
	}


}
